package com.lxj.ticket.service.common.dto;

import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class R<T> {

	private int code;

	private String message;

	private T data;

	private static final long serialVersionUID = 1L;

	public R() {
		this.code = 0;
		this.message = "消息";
	}

	public static <T> R<T> error() {
		return error(1, "操作失败");
	}

	public static <T> R<T> error(String msg) {
		return error(500, msg);
	}

	public static <T> R<T> error(int code, String msg) {
		return new R<T>().setCode(code).setMessage(msg);
	}

	/**
	 * 返回正确的数据
	 *
	 * @param msg
	 * @param t
	 * @return
	 */
	public static <T> R<T> ok(String msg, T t) {
		R<T> r = new R<>();
		r.setCode(0).setMessage(msg).setData(t);
		return r;
	}

	public static <T> R<T> ok(int code, String msg) {
		R<T> r = new R<>();
		r.setCode(code);
		r.setMessage(msg);
		return r;
	}

	public static <T> R<T> ok(T t) {
		return new R<T>()
				.setCode(0)
				.setMessage("操作成功")
				.setData(t);
	}

	public static <T> R<T> ok() {
		return new R<T>().setCode(0).setMessage("操作成功");
	}
}
